import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/presentation/public_home_page/models/public_home_data.dart';

import '../../apiClient/apis_data.dart';

class User extends Equatable{
  final int? id;
  final String? userId;
  final String? playerId;
  final String? coachId;
  final String? username;
  final String fullName;
  final String genderValue;
  final String type;
  final String? barcode;
  final String? email;
  String? phone;
  final String? password;
  String? image;
  final String? level;
  final String? ratingCount;
  final String? evaluation;
  final String? activeValue;
  final bool? public;
  String? description;
  String? power;
  String? agility;
  String? birthdate;
  String? commitment;
  final String? rate;
  final String? isDeleted;
  final DateTime? createAt;
  final DateTime? updateAt;
  final String? createdBy;
  final String? adminsSubscriptionsId;
  final String? token;
  File? imageFile;
  late double userRate;
  User({
    this.id,
    this.userId,
    this.playerId,
    this.coachId,
    this.username,
    required this.fullName,
    this.barcode,
    required this.type,
    required this.genderValue,
    this.email,
    this.phone,
    this.password,
    this.image,
    this.level,
    this.evaluation,
    this.activeValue,
    this.public,
    this.description,
    this.power,
    this.agility,
    this.birthdate,
    this.commitment,
    this.rate,
    this.ratingCount,
    this.isDeleted,
    this.createAt,
    this.updateAt,
    this.createdBy,
    this.adminsSubscriptionsId,
    this.token,
  }){
    if(DateTime.tryParse(birthdate.toString())!=null){
      const String dateTimeFormatPattern = 'yyyy-MM-dd';
      birthdate = DateTime.parse(birthdate.toString()).format(dateTimeFormatPattern);
    }
    if(image != null && image!.isEmpty){
      image = null;
    }
    if(phone!= null && phone!.isEmpty){
      phone = "لايوجد";
    }
    if(description == null || description!.isEmpty){
      description = "لا يوجد وصف";
    }
    commitment = (double.tryParse(commitment.toString())?.toStringAsFixed(2))??'0';
    power = (double.tryParse(power.toString())?.toStringAsFixed(2))??'0';
    agility = (double.tryParse(agility.toString())?.toStringAsFixed(2))??'0';
    userRate= (double.tryParse(commitment.toString()) ?? 0) +
        (double.tryParse(agility.toString()) ?? 0) +  (double.tryParse(agility.toString()) ?? 0);
    userRate /= 3;
  }

  String get gender {
    if (genderValue == '0') {
      return 'ذكر';
    } else if (genderValue == '1') {
      return 'أنثى';
    } else {
      return 'غير محدد';
    }
  }
  String get active {
    if (activeValue == '1') {
      return 'نشط';
    } else if (activeValue == '0') {
      return 'معلق';
    } else {
      return 'غير محدد';
    }
  }

  User copyWith({
    String? userId,
    String? playerId,
    String? coachId,
    String? username,
    String? fullName,
    String? genderValue,
    String? type,
    String? barcode,
    String? email,
    String? phone,
    String? password,
    String? image,
    String? level,
    String? evaluation,
    String? activeValue,
    bool? public,
    String? description,
    String? power,
    String? agility,
    String? birthdate,
    String? commitment,
    String? rate,
    String? ratingCount,
    String? isDeleted,
    DateTime? createAt,
    DateTime? updateAt,
    String? createdBy,
    String? admins_subscriptions_id,
    String? token,
    File? imageFile,
    double? userRate,
  }) {
    return User(
      userId: userId ?? this.userId,
      playerId: playerId ?? this.playerId,
      coachId: coachId ?? this.coachId,
      username: username ?? this.username,
      fullName: fullName ?? this.fullName,
      genderValue: genderValue ?? this.genderValue,
      type: type ?? this.type,
      barcode: barcode ?? this.barcode,
      email: email ?? this.email,
      phone: phone ?? this.phone,
      password: password ?? this.password,
      image: image ?? this.image,
      level: level ?? this.level,
      evaluation: evaluation ?? this.evaluation,
      activeValue: activeValue ?? this.activeValue,
      public: public ?? this.public,
      description: description ?? this.description,
      power: power ?? this.power,
      agility: agility ?? this.agility,
      birthdate: birthdate ?? this.birthdate,
      commitment: commitment ?? this.commitment,
      rate: rate ?? this.rate,
      ratingCount: ratingCount ?? this.ratingCount,
      isDeleted: isDeleted ?? this.isDeleted,
      createAt: createAt ?? this.createAt,
      updateAt: updateAt ?? this.updateAt,
      createdBy: createdBy ?? this.createdBy,
      token: token ?? this.token,
    );
  }

  factory User.fromJson(Map<String, dynamic> jsonData) {

    return User(
      id:int.tryParse(jsonData['id'].toString()) ,
      userId: jsonData['user_id']?.toString(),
      playerId: jsonData['player_id']?.toString(),
      coachId: jsonData['coach_id']?.toString(),
      username: jsonData['username']??"غير محدد",
      fullName: jsonData['full_name']??"مجهول",
      email: jsonData['email']??"لا يوجد",
      phone: jsonData['phone']??"لا يوجد",
      password: jsonData['password']??"",
      barcode: jsonData['barcode']??"",
      image: jsonData['image']??"",
      level: jsonData['level']?.toString()??"",
      evaluation: jsonData['evaluation']?.toString()??"",
      activeValue: jsonData['active']?.toString()??"",
      public: jsonData['active'].toString() == '1'? true:false ,
      type: jsonData['type']?.toString()??"",
      description: jsonData['description']?.toString()??"",
      power: jsonData['power']?.toString()??"",
      agility: jsonData['agility']?.toString()??"",
      birthdate: jsonData['birthdate']?.toString()??"",
      commitment: jsonData['commitment']?.toString()??"",
      rate: jsonData['rate']?.toString()??"0.0",
      ratingCount: jsonData['rating_count']?.toString()??"",
      genderValue: jsonData['gender']?.toString()??"",
      isDeleted: jsonData['is_deleted']?.toString()??"",
      createAt: DateTime.parse(jsonData['create_at']),
      updateAt: DateTime.parse(jsonData['update_at']),
      createdBy: jsonData['created_by']?.toString()??"",
        adminsSubscriptionsId: jsonData['admins_subscriptions_id']?.toString()??"",
      token: jsonData['token']?.toString()
    );
  }

  factory User.empty() {

    return User(
      userId: '-1',
      username: "غير محدد",
      fullName: "غير محدد",
      barcode: "غير محدد",
      type: "غير محدد",
      genderValue: "غير محدد",
    );
  }

  Future<Map<String, dynamic>> toJson() async{
    Map<String, dynamic> jsonData = {
      'user_id': userId,
      'gym_id': APISData.homeData.exercise.first.gymId,
      'player_id': playerId,
      'coach_id': coachId,
      'username': username,
      'full_name': fullName,
      'email': email,
      'phone': phone,
      'password': password,
      'barcode': barcode,
      if(imageFile != null)
        'image':await MultipartFile.fromFile( imageFile!.path, filename: imageFile!.path.split('/').last ),
      'level': level,
      'evaluation': evaluation,
      'active': activeValue??1,
      'public': public==true?1:0,
      'type': type,
      'description': description,
      'power': power,
      'agility': agility,
      'birthdate': birthdate,
      'commitment': commitment,
      'rate': rate,
      'gender': genderValue,
      //'admins_subscriptions_id': PrefUtils.getAdminsSubscriptionsId(),
      "create_at": createAt?.toIso8601String(),
      "update_at": updateAt?.toIso8601String(),
      "created_by": createdBy
    };
    return jsonData;
  }

  Map<String, dynamic> toJsonLocale(){
    Map<String, dynamic> jsonData = {
      'user_id': userId,
      'username': username,
      'full_name': fullName,
      'email': email,
      'phone': phone,
      'password': password,
      'barcode': barcode,
      if(image != null)
        'image': image,
      'level': level,
      'evaluation': evaluation,
      'active': activeValue??1,
      'public': public==true?1:0,
      'type': type,
      'description': description,
      'power': power,
      'agility': agility,
      'birthdate': birthdate,
      'commitment': commitment,
      'rate': rate,
      'gender': genderValue,
      //'admins_subscriptions_id': PrefUtils.getAdminsSubscriptionsId(),
      "create_at": createAt?.toIso8601String(),
      "update_at": updateAt?.toIso8601String(),
      "created_by": createdBy
    };
    return jsonData;
  }

  @override
  List<Object?> get props => [userId];
}